package com.ghn.cocknovel.utils

import android.content.Context
import android.content.pm.PackageManager
import android.util.Log
import com.tencent.map.geolocation.TencentLocationManager
import java.io.Closeable
import java.io.IOException

class DemoUtils {

    /**
     * 返回坐标系名称
     */
    fun toString(coordinateType: Int): String?{
        return if (coordinateType == TencentLocationManager.COORDINATE_TYPE_GCJ02) {
            "国测局坐标(火星坐标)"
        } else if (coordinateType == TencentLocationManager.COORDINATE_TYPE_WGS84) {
            "WGS84坐标(GPS坐标, 地球坐标)"
        } else {
            "非法坐标"
        }
    }

    /**
     * 返回 manifest 中的 key
     */
    fun getKey(context: Context): String? {
        var key: String? = null
        try {
            val appInfo = context.packageManager
                .getApplicationInfo(
                    context.packageName,
                    PackageManager.GET_META_DATA
                )
            val metaData = appInfo.metaData
            if (metaData != null) {
                key = metaData.getString("TencentMapSDK")
            }
        } catch (e: PackageManager.NameNotFoundException) {
            Log.e(
                "TencentLocation",
                "Location Manager: no key found in manifest file"
            )
            key = ""
        }
        return key
    }

    fun closeQuietly(closeable: Closeable?) {
        if (closeable != null) {
            try {
                closeable.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
    }
}
